﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Microsoft.Office.Tools.Word;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Office = Microsoft.Office.Core;
using MSWord = Microsoft.Office.Interop.Word;

namespace intelliSys.Management.Automation.Test.Word
{
    public partial class ThisDocument
    {
        private RichTextBox tb;
        private void ThisDocument_Startup(object sender, System.EventArgs e)
        {
            MessageBox.Show("欢迎！接下来将简单介绍一下OpenXML for Word");
            MessageBox.Show("选择一些文本，然后单击右侧行动窗格，可以显示相应的XML文档");
            PictureBox pb = new PictureBox();
            Button button = new Button();
            button.Click += new EventHandler(button_Click);
            button.Text = "点击查看所选内容的XML文档";
            Button bt = new Button() { Text = "Copy Clipboard" };
            bt.Click += new EventHandler(bt_Click);
            this.ActionsPane.Controls.Add(button);
            this.ActionsPane.Controls.Add(bt);
        }

        void bt_Click(object sender, EventArgs e)
        {
            Clipboard.SetData("intelliSys.Management.Automation.Test", intelliSys.Management.Automation.Test.Interop.OpenXMLInteropHelper.Concat(this.ActiveWindow.Selection.Range.WordOpenXML, this.ActiveWindow.Selection.Range.Text));
            //  Clipboard.SetData("intelliSys.Management.Automation.Test.PlainText", this.Windows[1].Selection.Range.Text);
        }
        string t;
        MSWord.Range r;
        void button_Click(object sender, EventArgs e)
        {
            Button prev = sender as Button;
            prev.Click -= button_Click;
            prev.Click += new EventHandler(prev_Click);
            prev.Text = "刷新";
            Panel p = new Panel();
            tb = new RichTextBox();
            p.Height = 300;
            p.Width = 500;
            tb.Width = 500; ;
            tb.Height = 290;
            tb.WordWrap = false;
            r =
             this.Windows[1].Selection.Range;
            t = r.WordOpenXML;
            System.IO.File.CreateText("log").WriteLine(r.WordOpenXML);
            tb.Text = t;
            p.Controls.Add(tb);
            this.ActionsPane.Controls.Add(p);
            Button b = new Button();
            b.Click += new EventHandler(b_Click);
            b.Text = "关闭";
            this.ActionsPane.Controls.Add(b);
        }

        void prev_Click(object sender, EventArgs e)
        {
            MSWord.Range r =
          this.Windows[1].Selection.Range;
            MSWord.XMLNode xml = null;
            string t = r.WordOpenXML;
            tb.Text = t;
        }

        void b_Click(object sender, EventArgs e)
        {
            this.ActionsPane.Controls.Clear();
        }

        private void ThisDocument_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisDocument_Startup);
            this.Shutdown += new System.EventHandler(ThisDocument_Shutdown);
        }

        #endregion
    }
}
